﻿
namespace TFSChangeLog.DataAccess
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>Summary description for <see cref="TFSChangeLogStaticConfigs"/> class.</summary>
    public static class TFSChangeLogStaticConfigs
    {
        /// <summary>Gets or sets a value indicating whether IsRunningFromCommandPrompt is true or false.</summary>
        public static bool IsRunningFromCommandPrompt { get; set; }

        /// <summary>Gets or sets a value indicating whether IsRunningInSilentMode is true or false.</summary>
        public static bool IsRunningInSilentMode { get; set; }

        /// <summary>Gets or sets a value indicating whether IsIgnoreXSLT is true or false.</summary>
        public static bool IsIgnoreXSLT { get; set; }

        /// <summary>Gets the value for ApplicationLoadDateTime.</summary>
        public static string ApplicationLoadDateTime { get; private set; }

        /// <summary>Gets the value for TFSChangeLogHTMLFileName.</summary>
        public static string TFSChangeLogHTMLFileName { get; private set; }

        /// <summary>Gets the value for TFSChangeLogXMLFileName.</summary>
        public static string TFSChangeLogXMLFileName { get; private set; }

        /// <summary>Gets or sets the value for tfsChangeLogOuputDirectory.</summary>
        public static string tfsChangeLogOuputDirectory { get; set; }

        /// <summary>
        /// xml changes file name.
        /// </summary>
        public static string TFSConfigChangesFileName { get; private set; }

        /// <summary>
        /// output file name.
        /// </summary>
        public static string TFSOutputFileName { get; private set; }

        /// <summary>Summary description for <c>TFSChangeLogStaticConfigs</c> method.</summary>
        public static void Initialize()
        {
            IsRunningFromCommandPrompt = false;
            IsRunningInSilentMode = false;
            IsIgnoreXSLT = false;

            DateTime dt = DateTime.Now;
            ApplicationLoadDateTime = string.Format("{0:yyyyMMdd_HHmmss}", dt);

            // var tfsChangeLogOuputDirectory = GetLocalAppDataPath();
            GetLocalAppDataPath();

            if (tfsChangeLogOuputDirectory != null)
            {
                TFSChangeLogHTMLFileName = tfsChangeLogOuputDirectory + ApplicationLoadDateTime + "_TFSChangeLog.html";
                TFSChangeLogXMLFileName = tfsChangeLogOuputDirectory + ApplicationLoadDateTime + "_TFSChangeLog.xml";
                TFSConfigChangesFileName = tfsChangeLogOuputDirectory + ApplicationLoadDateTime + "_Temp.txt";
                TFSOutputFileName = tfsChangeLogOuputDirectory + ApplicationLoadDateTime + "_ReleaseNotes.doc";
            }
            else
            {
                TFSChangeLogHTMLFileName = ApplicationLoadDateTime + "_TFSChangeLog.html";
                TFSChangeLogXMLFileName = ApplicationLoadDateTime + "_TFSChangeLog.xml";
                TFSConfigChangesFileName = ApplicationLoadDateTime + "_Temp.txt";
                TFSOutputFileName = ApplicationLoadDateTime + "_ReleaseNotes.doc";
            }
        }

        /// <summary>Summary description for <c>TFSChangeLogStaticConfigs</c> method.</summary>
        private static void GetLocalAppDataPath()
        {
            tfsChangeLogOuputDirectory = Environment.GetEnvironmentVariable("LOCALAPPDATA");

            if (tfsChangeLogOuputDirectory == null)
            {
                Console.WriteLine("WARNING                : Environment Variable \"LOCALAPPDATA\" is not set.");
                Console.WriteLine("                       : Application will try to save ChangeLog output in the application root directory.");
            }
            else
            {
                if (!(tfsChangeLogOuputDirectory.EndsWith("/") || tfsChangeLogOuputDirectory.EndsWith("\\")))
                {
                    tfsChangeLogOuputDirectory = tfsChangeLogOuputDirectory + "/";
                }

                tfsChangeLogOuputDirectory = tfsChangeLogOuputDirectory + "TFSChangeLog/";
                if (System.IO.Directory.Exists(tfsChangeLogOuputDirectory) == false)
                    {
                    System.IO.Directory.CreateDirectory(tfsChangeLogOuputDirectory);
                    }
            }
        }

        /// <summary>Summary description for <c>TFSChangeLogStaticConfigs</c> method.</summary>
        public static void ResetDateTimeOnOutputFileNames()
        {
            DateTime dt = DateTime.Now;
            ApplicationLoadDateTime = string.Format("{0:yyyyMMdd_HHmmss}", dt);

            if (tfsChangeLogOuputDirectory != null)
            {
                TFSChangeLogHTMLFileName = tfsChangeLogOuputDirectory + ApplicationLoadDateTime + "_TFSChangeLog.html";
                TFSChangeLogXMLFileName = tfsChangeLogOuputDirectory + ApplicationLoadDateTime + "_TFSChangeLog.xml";
                TFSConfigChangesFileName = tfsChangeLogOuputDirectory + ApplicationLoadDateTime + "_Temp.txt";
                TFSOutputFileName = tfsChangeLogOuputDirectory + ApplicationLoadDateTime + "_ReleaseNotes.doc";
            }
            else
            {
                TFSChangeLogHTMLFileName = ApplicationLoadDateTime + "_TFSChangeLog.html";
                TFSChangeLogXMLFileName = ApplicationLoadDateTime + "_TFSChangeLog.xml";
                TFSConfigChangesFileName = ApplicationLoadDateTime + "_Temp.txt";
                TFSOutputFileName = ApplicationLoadDateTime + "_ReleaseNotes.doc";
            }
        }
    }
}
